package com.information.manage.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.information.manage.basic.common.result.Result;
import com.information.manage.basic.model.entity.DictionaryCategoryDO;
import com.information.manage.basic.model.query.AddDictionaryCategoryQuery;
import com.information.manage.basic.model.query.DictPageQuery;
import com.information.manage.basic.model.vo.DictionaryCategoryListVO;
import com.information.manage.basic.mapper.DictionaryCategoryMapper;
import com.information.manage.basic.service.DictionaryCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DictionaryCategoryServiceImpl extends ServiceImpl<DictionaryCategoryMapper, DictionaryCategoryDO> implements DictionaryCategoryService {

    @Resource
    private DictionaryCategoryMapper dictionaryCategoryMapper;

    @Override
    public List<DictionaryCategoryListVO> queryDictionaryCategoryList(String name) {
        List<DictionaryCategoryListVO> dictionaryCategoryListVOS = dictionaryCategoryMapper.queryDictionaryCategoryList(name);
        return dictionaryCategoryListVOS;
    }

    @Override
    public Result<Long> add(AddDictionaryCategoryQuery addDictionaryCategoryQuery) {
        DictionaryCategoryDO dictionaryCategoryDO = new DictionaryCategoryDO();
        BeanUtils.copyProperties(addDictionaryCategoryQuery, dictionaryCategoryDO);
        dictionaryCategoryMapper.insert(dictionaryCategoryDO);
        return Result.ok(Long.valueOf(dictionaryCategoryDO.getId()));
    }

    @Override
    public Result<Page<DictionaryCategoryDO>> queryDictPage(DictPageQuery dictPageQuery) {
        Page<DictionaryCategoryDO> page=new Page<>(dictPageQuery.getCurPage(),dictPageQuery.getPageSize());
        QueryWrapper<DictionaryCategoryDO> queryWrapper=new QueryWrapper<>();
        if(dictPageQuery.getName()!=null){
            queryWrapper.like("name","%"+dictPageQuery.getName()+"%");
        }
        return Result.ok(this.page(page,queryWrapper));
    }

}
